<template>
  <div class="live">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>直播管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="24">
        <el-col :span="8">
          <span>直播状态：</span>
          <el-select
            v-model="value"
            @change="changeOption"
            placeholder="请选择"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.label"
            >
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="8">
          <span>经纪人搜索：</span
          ><el-input
            v-model="queryInfo.keyword"
            placeholder="姓名、id"
          ></el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="findKeyWord">查询</el-button>
        </el-col>
      </el-row>
    </el-card>
    <div class="live_main">
      <h3>全部直播</h3>
      <div class="live_main_main">
        <dl class="live_main_box" v-for="v in result.length ? result :tableData" :key="v.id">
          <dt><img :src="v.img" alt="" /></dt>
          <dd>
            <p>{{ v.title }}</p>
            <p style="color: #999;">
              <span>{{ v.name }}</span>
              <span>{{ v.create_time }}</span>
            </p>
            <p class="live_type" v-show="v.type === 'true'">
              <span class="detail" @click="$router.push(`/livedetail/${v.id}`)">进入直播</span>
              <el-switch
                v-model="v.state"
                class="isShow"
                :active-value="'1'"
                :inactive-value="'0'"
                active-color="#13ce66"
                inactive-color="#ff4949"
                @change="changeSwitch(v)"
              >
              </el-switch>
            </p>
            <p class="live_type" v-show="v.type === 'false'">
              <span>回看</span>
              <span class="delete" @click="delLiveList(v.id)">删除</span>
            </p>
          </dd>
        </dl>
      </div>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageIndex"
        :page-sizes="[12, 20, 30]"
        :page-size="queryInfo.pageSize"
        layout="sizes, prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      value: '',
      queryInfo: {
        keyword: '',
        pageIndex: 1,
        pageSize: 12
      },
      tableData: [],
      result: [],
      total: 0,
      options: [
        {
          value: '选项1',
          label: '请选择'
        },
        {
          value: '选项2',
          label: '直播中'
        },
        {
          value: '选项3',
          label: '已结束'
        }
      ]
    }
  },
  methods: {
    changeOption (val) {
      this.tableData.map((item) => {
        if (val === '直播中') {
          this.result = this.tableData.filter((item) => {
            if (item.type === 'true') {
              return item
            }
          })
        } else if (val === '已结束') {
          this.result = this.tableData.filter((item) => {
            if (item.type === 'false') {
              return item
            }
          })
        } else {
          this.result = []
        }
      })
      // this.result = this.tableData.filter(item => {
      //   return item.type.includes(val)
      // })
    },
    findKeyWord () {
      this.getLiveList(this.queryInfo)
    },
    changeSwitch (row) {
      this.$http.put('/live/edit/type', row).then((res) => {
        if (res.data.code !== 1) {
          this.$message.error(res.data.message)
        } else {
          this.$message.success(res.data.message)
        }
      })
    },
    getLiveList () {
      this.$http
        .get('/live', {
          params: this.queryInfo
        })
        .then((res) => {
          if (res.data.code === 1) {
            this.tableData = res.data.data.data
            this.total = res.data.data.total
          }
        })
    },
    delLiveList (id) {
      this.$http.delete(`/live/${id}`).then((res) => {
        if (res.data.code !== 1) {
          this.$message.error(res.data.message)
        }
        this.$message.success(res.data.message)
        this.getLiveList(this.queryInfo)
      })
    },
    handleSizeChange (newsize) {
      this.queryInfo.pageSize = newsize
      this.getLiveList(this.queryInfo)
    },
    handleCurrentChange (current) {
      this.queryInfo.pageIndex = current
      this.getLiveList(this.queryInfo)
    }
  },
  created () {
    this.getLiveList()
  }
}
</script>

<style scoped>
.el-row,
.el-col {
  display: flex;
  align-items: center;
}

.el-col:nth-child(2) > span {
  width: 138px;
}

.tableData {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
}
.delete:hover,
.detail:hover {
  cursor: pointer;
}
.live_main {
  width: 100%;
  height: auto;
  background: #fff;
}

.live_main h3 {
  padding: 10px;
}

.live_main_main {
  padding: 0 10px;
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}

.live_main_box {
  width: 15%;
}

.live_type {
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: blue;
}

.live_main_box dt {
  width: 100%;
  height: 100px;
  margin-bottom: 10px;
}

.live_main_box dd {
  width: 100%;
  font-size: 12px;
  line-height: 24px;
}

.live_main_box dd p {
  display: flex;
  align-items: center;
}

.live_main_box dt img {
  width: 100%;
  height: 100px;
}
</style>
